<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\read_uv;


use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\logic\auth\AdminLogic;
use app\common\model\book\Book;
use app\common\model\promotion\PromotionLink;
use app\common\model\read_uv\ReadUv;
use app\common\lists\ListsSearchInterface;
use app\common\service\FileService;


/**
 * ReadUv列表
 * Class ReadUvLists
 * @package app\adminapi\lists
 */
class BookUvLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/04/22 15:52
     */
    public function setSearch(): array
    {
        return [
            '%like%' => ['b.book_name'],
            '=' => ['b.id','ru.platform'],
        ];
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/04/22 15:52
     */
    public function lists(): array
    {
        $bookList = ReadUv::alias('ru')->join("book b", 'b.id = ru.book_id', 'inner')
            ->field('ru.book_id, MAX(chapter_id) AS chapter_id, MAX(ru.update_time) AS update_time,b.id, b.book_name, b.thumb, b.author, b.book_type')
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->group('book_id')
            ->limit($this->limitOffset, $this->limitLength)
            ->order('id desc')
            ->select()->toArray();
        foreach ($bookList as &$item){
            $item['thumb'] = FileService::getFileUrl($item['thumb']);
        }
        return $bookList;
    }

    /**
     * @notes 搜索条件
     * @author 段誉
     * @date 2023/2/24 16:08
     */
    public function queryWhere()
    {
        $where = [];
        // 下单时间
        if (!empty($this->params['start_time']) && !empty($this->params['end_time'])) {
            $time    = [strtotime($this->params['start_time']), strtotime($this->params['end_time'])];
            $where[] = ['ru.update_time', 'between', $time];
        }

//        if ($this->adminInfo['root'] != 1) {
//            $pAdminIds = AdminLogic::getPermissionAdminIds($this->adminInfo);
//            if ($pAdminIds) {
//                $promotion_link_id_arr = PromotionLink::where('admin_id','in',$pAdminIds)->column('id');
//                if ($promotion_link_id_arr){
//                    $where[] = ['promotion_link_id', 'in', $promotion_link_id_arr];
//                }
//            }
//        }

        return $where;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/04/22 15:52
     */
    public function count(): int
    {
        return ReadUv::alias('ru')->join("book b", 'b.id = ru.book_id', 'inner')
            ->field('ru.book_id, MAX(chapter_id) AS chapter_id, MAX(ru.update_time) AS update_time,b.id, b.book_name, b.thumb, b.author, b.book_type')
            ->where($this->searchWhere)
            ->where($this->queryWhere())->group('book_id')->select()->count();
    }

}